<?php
/**
 * Created by PhpStorm.
 * User: 王崇全
 * Date: 2017/12/16
 * Time: 10:01
 */

namespace app\common\behavior;

use think\facade\App;
use think\facade\Env;
use think\facade\Request;

class Index
{

    public function moduleInit()
    {
        define("MODULE", Request::module());
        define("CONTROLLER", Request::controller());
        define("ACTION", Request::action());
        define("URL_PATH", MODULE."/".CONTROLLER."/".ACTION);

        // 适配 TP5.0
        defined("DS") or define("DS", DIRECTORY_SEPARATOR);
        defined("THINK_START_TIME") or define("THINK_START_TIME", App::getBeginTime());
        defined("THINK_START_MEM") or define("THINK_START_MEM", App::getBeginMem());
        defined("THINK_VERSION") or define("THINK_VERSION", App::version());
        defined("THINK_PATH") or define("THINK_PATH", Env::get('think_path'));
        defined("LIB_PATH") or define("LIB_PATH", Env::get('think_path').'library/');
        defined("CORE_PATH") or define("CORE_PATH", Env::get('think_path').'library/think/');
        defined("APP_PATH") or define("APP_PATH", Env::get('app_path'));
        defined("CONFIG_PATH") or define("CONFIG_PATH", Env::get('config_path'));
        defined("CONFIG_EXT") or define("CONFIG_EXT", App::getConfigExt());
        defined("ROOT_PATH") or define("ROOT_PATH", Env::get('root_path'));
        defined("EXTEND_PATH") or define("EXTEND_PATH", Env::get('root_path').'extend/');
        defined("VENDOR_PATH") or define("VENDOR_PATH", Env::get('root_path').'vendor/');
        defined("RUNTIME_PATH") or define("RUNTIME_PATH", Env::get('runtime_path'));
        defined("LOG_PATH") or define("LOG_PATH", Env::get('runtime_path').'log/');
        defined("CACHE_PATH") or define("CACHE_PATH", Env::get('runtime_path').'cache/');
        defined("TEMP_PATH") or define("TEMP_PATH", Env::get('runtime_path').'temp/');
        defined("MODULE_PATH") or define("MODULE_PATH", Env::get('module_path'));

        //api模块返回json数据
        //if (strtoupper(MODULE) == "API" || strtoupper(MODULE) == "ADMIN") {
        header('Content-type: application/json');

        config("app.default_return_type", "json");

        header("Access-Control-Allow-Origin: ".UI_URL); // 允许任意域名发起的跨域请求
        header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With,X_Requested_With');
        header('Access-Control-Allow-Method:GET, POST'); //允许访问的方式
        header("Access-Control-Allow-Credentials: true"); //是否允许请求带有验证信息
        header("Access-Control-Max-Age:".ACCESS_CONTROL_MAX_AGE); //在这个时间范围内，所有同类型的请求都将不再发送预检请求而是直接使用此次返回的头作为判断依据
        header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"'); //通过设置P3P头来实现跨域访问COOKIE

        if (Request::isOptions()) {
            rj(0,"OPTIONS OK");
        }

        //}

    }

}